<?php

namespace App\Services;

use App\Models\Category\Category;
use App\Models\ProductAttr;
use App\Models\ProductAttrResult;
use App\Models\ProductAttrValue;
use App\Models\ProductCate;
use App\Models\ProductCoupon;
use App\Models\ProductDescription;
use App\Models\ProductUserLabelRelation;

class ProductService
{
    /**
     * 商品分类中间表添加
     */
    public function storeProductcate($category, $model)
    {
        //将字符串拆分成数组
        $categories = array_filter(explode(',', $category));

        //查询到每个分类的顶级分类
        $top_category_ids = Category::query()->whereIn('id', $categories)->get('top_id');

        //进行数组重组，方便插入
        $product_cate = [];
        foreach ($categories as $key => $category_id) {
            $product_cate[] = [
                'product_id' => $model->id,
                'category_id' => $category_id,
                'top_category_id' => $top_category_ids[$key]->top_id,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
        }

        //执行中间表插入
        $product_cate_model = new ProductCate();
        $product_cate_bool = $product_cate_model->insert($product_cate);

        return $product_cate_bool;
    }


    /**
     * 商品多规格属性表添加
     */

    public function storeProductattr($attrs, $model)
    {
        //进行数组重组，方便插入
        $attrsss = [];
        foreach ($attrs as $attr) {
            $attr['product_id'] = $model->id;
            $attr['created_at'] = date('Y-m-d H:i:s');
            $attr['updated_at'] = date('Y-m-d H:i:s');
            $attrsss[] = $attr;
        }

        //执行商品属性表插入
        $product_attr = new ProductAttr();
        $product_attr_bool = $product_attr->insert($attrsss);

        return $product_attr_bool;
    }


    /**
     * 商品多规格属性详情表添加
     */
    public function storeProductattrresult($attr_results, $model)
    {
        //进行数组重组，方便插入
        $attr_resultsss = [];
        foreach ($attr_results as $result) {
            $attr_result['product_id'] = $model->id;
            //用insert添加先转换json为字符串
            $result = json_encode($result);
            $attr_result['result'] = $result;
            $attr_result['created_at'] = date('Y-m-d H:i:s');
            $attr_result['updated_at'] = date('Y-m-d H:i:s');
            $attr_resultsss[] = $attr_result;
        }

        //执行商品属性表插入
        $product_attr_result = new ProductAttrResult();
        $product_attr_result_bool = $product_attr_result->insert($attr_resultsss);

        return $product_attr_result_bool;
    }


    /**
     * 商品多规格属性详情表添加
     */
    public function storeProdcutattrvalue($product_attr_values, $model)
    {
        $product_attr_values['product_id'] = $model->id;
        $product_attr_values['created_at'] = date('Y-m-d H:i:s');
        $product_attr_values['updated_at'] = date('Y-m-d H:i:s');

        //执行商品单轨格属性表插入
        $product_attr_values_model = new ProductAttrValue();
        $product_attr_values_bool = $product_attr_values_model->insert($product_attr_values);

        return $product_attr_values_bool;
    }


    /**
     * 商品详情表添加
     */
    public function storeProductdescription($description, $model)
    {
        $descriptions['product_id'] = $model->id;
        $descriptions['description'] = $description;
        $descriptions['created_at'] = date('Y-m-d H:i:s');
        $descriptions['updated_at'] = date('Y-m-d H:i:s');

        //执行商品属性表插入
        $product_descriptions = new ProductDescription();
        $product_descriptions_bool = $product_descriptions->insert($descriptions);

        return $product_descriptions_bool;
    }


    /**
     * 商品 用户标签 中间表添加
     */
    public function storeProductuserlabel($product_user_label, $model)
    {
        $product_user_labels = array_filter(explode(',', $product_user_label));

        $resultss = [];
        foreach ($product_user_labels as $user_label_id) {
            $label['product_id'] = $model->id;
            $label['user_label_id'] = $user_label_id;
            $label['created_at'] = date('Y-m-d H:i:s');
            $label['updated_at'] = date('Y-m-d H:i:s');
            $resultss[] = $label;
        }

        //执行中间表插入
        $product_user_label_model = new ProductUserLabelRelation();
        $product_user_label_bool = $product_user_label_model->insert($resultss);

        return $product_user_label_bool;
    }


    /**
     * 商品赠送优惠券关联表添加
     */
    public function storeProductissuecouponids($issue_coupon_ids, $model)
    {
        $resultss = [];
        foreach ($issue_coupon_ids as $issue_coupon_id) {
            $issue_coupon['product_id'] = $model->id;
            $issue_coupon['issue_coupon_id'] = $issue_coupon_id;
            $issue_coupon['created_at'] = date('Y-m-d H:i:s');
            $issue_coupon['updated_at'] = date('Y-m-d H:i:s');
            $resultss[] = $issue_coupon;
        }

        //执行关联表插入
        $product_coupon_model = new ProductCoupon();
        $product_coupon_bool = $product_coupon_model->insert($resultss);

        return $product_coupon_bool;
    }
}
